//
//  MyMassageViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/7.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "MyMassageViewController.h"
#import "MyMassageTableViewCell.h"
#import "MyMassageHeadView.h"
#import "UIView+BlockGesture.h"
#import "CouponViewController.h"
#import "MessageViewController.h"
#import "TimeViewController.h"
#import "CustomerViewController.h"
#import "MyIncomeViewController.h"
#import "SetAddressViewController.h"
#import "ENAlert.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "ChangePasswordViewController.h"
@interface MyMassageViewController ()
{
    NSArray *_imgArray;
    NSArray *_titleArray;
}
@end

@implementation MyMassageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"我的" fontSize:18];
    _titleArray=@[@"我的客户",@"地址设置",@"查看可预约时间",@"修改密码",@"版本信息",@"退出登录"];
    _imgArray=@[@"icon_kehu@2x",@"icon_dizhishezhi@2x_17",@"icon_chakanshijian@2x",@"icon_xiugaimima@2x",@"icon_banbenxinxi@2x",@"icon_tuichudenglu@2x"];
    __weak typeof(self) weakSelf = self;

    [self setLeftImageBarButtonItemWithFrame:CGRectMake(0, 0, 28, 23) image:@"icon_tongzhi" selectImage:nil action:^(WRBButton *button) {
        [weakSelf message:button];
        
    }];

    [self customTableView];
    UINib *nib=[UIView loadNibNamed:@"MyMassageTableViewCell" bundle:nil];
    
    [self.tableview registerNib:nib forCellReuseIdentifier:@"MyMassageTableViewCell"];
    // Do any additional setup after loading the view from its nib.
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [AppDelegate shareInstance].rootNav.navigationBar.hidden=YES;

    self.navigationController.navigationBarHidden=NO;

}
-(void)message:(WRBButton *)btn
{
    ALLOC(MessageViewController, view);
    view.hidesBottomBarWhenPushed=YES;

    [self pushNextViewController:view];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 46;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
           return 124;

    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
           MyMassageHeadView *headerView=[[MyMassageHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 124)];
    [headerView.inComeView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
              NSLog(@"===");
        
        ALLOC(MyIncomeViewController, view);
        view.hidesBottomBarWhenPushed=YES;
        [self pushNextViewController:view];
    }];
    [headerView.youHuiQuanView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        
        ALLOC(CouponViewController, view);
        view.hidesBottomBarWhenPushed=YES;
        [self pushNextViewController:view];
        NSLog(@"===");
    }];

    
        return headerView;
      
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyMassageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyMassageTableViewCell" forIndexPath:indexPath];
    cell.title.text=_titleArray[indexPath.row];
    cell.headerImg.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",_imgArray[indexPath.row]]];
    if (indexPath.row==4) {
        cell.subTitle.hidden=NO;
        
        cell.subTitle.text=@"1.0";
    }
    else{
        cell.subTitle.hidden=YES;
        
    }if (indexPath.row>=4) {
        cell.jianTouImg.hidden=YES;

    }
    
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"请重写方法");
    
//      UITouch *touch =self.tableview to;
//    
//    CGPoint location = [touch locationInView:self.tableview];
//    NSIndexPath * indexPathx = [self.tableview indexPathForRowAtPoint:location];
    if (indexPath.row==0) {
        ALLOC(CustomerViewController, view);
        view.hidesBottomBarWhenPushed=YES;
        [self pushNextViewController:view];
        
    }
    if (indexPath.row==1) {
        ALLOC(SetAddressViewController, view);
        view.hidesBottomBarWhenPushed=YES;
        [self pushNextViewController:view];
        
    }

    
    if (indexPath.row==2) {
        ALLOC(TimeViewController, view);
        view.hidesBottomBarWhenPushed=YES;
        
        [self pushNextViewController:view];
        
    }
    if (indexPath.row==3) {
        ALLOC(ChangePasswordViewController, view);
        view.hidesBottomBarWhenPushed=YES;
        
        [self pushNextViewController:view];
        
    }
    if (indexPath.row==5) {
        
        
      
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                            message:@"是否退出"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确定", nil];
        [alertView show];
        

        
    }

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex==1) {
        ALLOC(LoginViewController, login);
        login.hidesBottomBarWhenPushed=YES;
        login.isFromMassageViewCOntroller=YES;
        [[TMCache sharedCache]removeAllObjects];
        [self clearCacheFiles];
        [self presentViewController:login animated:YES completion:nil];
    }
}

/*!
 *  @author WS, 16-01-15 18:01:25
 *
 *  退出登录清除缓存
 */
-(void)clearCacheFiles
{
    //清楚缓存
    dispatch_async(
                   
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
                   
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)objectAtIndex:0];
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       
                       NSLog(@"files :%lu",(unsigned long)[files count]);
                       
                       for (NSString *p in files) {
                           //如有需要，加入条件，过滤掉不想删除的文件
                           
                           NSError *error;
                           
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                               
                           }
                           
                       }
                       
                       
                       
                   });
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
